﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

/// <summary>
/// 音频管理类
/// </summary>
public class AudioManager : MonoBehaviour
{
    #region 实现AudioManager类的单例
    private static AudioManager _instance;
    private AudioManager() { }
    public static AudioManager Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject go = new GameObject();
                _instance = go.AddComponent<AudioManager>();
                go.name = "AudioManager";
                go.transform.parent = GameInit.GameManager.transform;
            }
            return _instance;
        }
    }
    #endregion

    //游戏背景音乐播放器
    AudioSource _BGMPlayer = null;
    //UI点击音效播放器
    AudioSource _BtnPlayer = null;
    //游戏中其他音效播放器
    AudioSource _GamePlayer = null;

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init()
    {
        //添加3个音频播放器
        _BGMPlayer = new GameObject("BGMPlayer").AddComponent<AudioSource>();
        _BGMPlayer.playOnAwake = false;
        _BGMPlayer.loop = true;
        _BtnPlayer = new GameObject("ClickPlayer").AddComponent<AudioSource>();
        _BtnPlayer.playOnAwake = false;
        _GamePlayer = new GameObject("GamingPlayer").AddComponent<AudioSource>();
        _GamePlayer.playOnAwake = false;

        _BGMPlayer.transform.parent = transform;
        _BtnPlayer.transform.parent = transform;
        _GamePlayer.transform.parent = transform;
        //恢复本地保存的设置参数 声音大小
        _BGMPlayer.volume = PlayerPrefs.GetFloat(Constant.BACKGROUND_MUSIC_VOLUME, 0.7f);
        _BtnPlayer.volume = PlayerPrefs.GetFloat(Constant.BUTTONCLICK_EFFECT_VOLUME, 0.7f);
        _GamePlayer.volume = PlayerPrefs.GetFloat(Constant.GAMING_EFFECT_VOLUME, 0.7f);

        //Debug.Log("AudioManager Init!");
    }

    /// <summary>
    /// 设置/获得背景音乐大小
    /// </summary>
    /// <param name="val"></param>
    public float BGMusicVolume
    {
        get { return _BGMPlayer.volume; }
        set
        {
            if (value >= 0 && value <= 1)
            {
                _BGMPlayer.volume = value;
                PlayerPrefs.SetFloat(Constant.BACKGROUND_MUSIC_VOLUME, value);
            }
        }
    }
    /// <summary>
    /// 设置/获得UI点击音效大小
    /// </summary>
    /// <param name="val"></param>
    public float ClickEffectVolume
    {
        get { return _BtnPlayer.volume; }
        set
        {
            if (value >= 0 && value <= 1)
            {
                _BtnPlayer.volume = value;
                PlayerPrefs.SetFloat(Constant.BUTTONCLICK_EFFECT_VOLUME, value);
            }
        }
    }
    /// <summary>
    /// 设置/获得游戏中其他音乐声音大小
    /// </summary>
    /// <param name="val"></param>
    public float GamingEffectVolume
    {
        get { return _GamePlayer.volume; }
        set
        {
            if (value >= 0 && value <= 1)
            {
                _GamePlayer.volume = value;
                PlayerPrefs.SetFloat(Constant.GAMING_EFFECT_VOLUME, value);
            }
        }
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="rel_path">相对[Assets/Module]的路径</param>
    /// <param name="play">切换后是否播放</param>
    public void PlayBGMuisc(string rel_path = "", bool play = true)
    {
        if (rel_path == "") rel_path = Constant.DEFAULT_BGM;

        AudioClip ac = LoadAssetManager.Instance.LoadClip(rel_path);

        _BGMPlayer.Stop();
        _BGMPlayer.clip = ac;
        if (play)
        {
            _BGMPlayer.Play();
        }
    }

    /// <summary>
    /// 暂停/播放背景音乐
    /// </summary>
    /// <param name="play"></param>
    public void ToggleBGMuisc()
    {
        if (_BGMPlayer.isPlaying)
        {
            _BGMPlayer.Stop();
        }
        else
        {
            _BGMPlayer.Play();
        }
    }

    /// <summary>
    /// 播放点击音效
    /// </summary>
    /// <param name="rel_path">相对[Assets/Module]的路径</param>
    public void PlayBtnClickEffect(string rel_path)
    {
        AudioClip ac = LoadAssetManager.Instance.LoadClip(rel_path);

        _BtnPlayer.clip = ac;
        _BtnPlayer.Play();
    }

    /// <summary>
    /// 播放游戏音效
    /// </summary>
    /// <param name="rel_path">相对[Assets/Module]的路径</param>
    public void PlayGamingEffect(string rel_path)
    {
        AudioClip ac = LoadAssetManager.Instance.LoadClip(rel_path);
        _BtnPlayer.clip = ac;

        _GamePlayer.Play();
    }
}
